<?php
include_once('adulte.class.php');
include_once('myPDO.class.php');
class AdulteDAO {
    
        // Création d'un adulte
	public function createAdulte(/* Adulte */ $adulteC, /* myPDO */ $db) {
		try {
			$stmt = $db->prepare("INSERT INTO Adulte (nom, prenom,mdp,email,imgProfil,estAdmin,estConfirme ) VALUES (:nom, :prenom, :mdp,:email,:img,0,0)");
			$nom = utf8_decode($adulteC->getNom());
			$prenom = utf8_decode($adulteC->getPrenom());
                        $mdp = utf8_decode($adulteC->getPassword());
                        $email = utf8_decode($adulteC->getEmail());
                        $img = utf8_decode($adulteC->getImage());
			$stmt->bindParam(':nom', $nom);
			$stmt->bindParam(':prenom',$prenom);
                        $stmt->bindParam(':mdp',$mdp);
                        $stmt->bindParam(':email',$email);
                        $stmt->bindParam(':img',$img);
                       
			$stmt->execute();
		} catch(Exception $e) {
			echo 'Exception re�ue : ',  $e->getMessage(), "\n";
		}
	}
        
        
        // Trouver un adulte selon le nom et le prenom ou le email
        public function find ( $adulte , $db) {
            
        $stmt = $db->prepare("SELECT * FROM Adulte WHERE (nom = :nom AND prenom = :prenom) OR (email = :email);");
        $nom = utf8_decode($adulte->getNom());
        $prenom = utf8_decode($adulte->getPrenom());
        $email = utf8_decode($adulte->getEmail());
        $stmt->bindParam(':nom', $nom);
        $stmt->bindParam(':prenom', $prenom);
        $stmt->bindParam (':email',$email);
        $res = $stmt->execute();
        $row = $stmt->fetchObject();
        if ($row == null) {
            return 0;
        } else {
           
            return 1;
        }
    }
    
    
        // Trouver les autres adultes autre que l'admin
	public function findOtherAdulte(/* Adulte */ $adulte, /* myPDO */ $db) {
		$stmt = $db->prepare("SELECT * FROM Adulte WHERE identifiant != :id AND email = :mail;");
		$id = $adulte->getIdentifiant();
                $email = utf8_decode($adulte->getEmail());
		$stmt->bindParam(':id', $id);
		$stmt->bindParam(':mail', $email);
		$res = $stmt->execute();
		$row = $stmt->fetchObject();
		if($row == null) {
			return 0 ;
		}
		else {
                    return 1;
		}
	}
        
        
        // Trouver un adulte selon l'id
	public function findAdulte(/* Adulte */ $adulte, /* myPDO */ $db) {
		$stmt = $db->prepare("SELECT * FROM Adulte WHERE identifiant = :id ;");
		$id = $adulte->getIdentifiant();
		$stmt->bindParam(':id', $id);
		$res = $stmt->execute();
		$row = $stmt->fetchObject();
		if($row == null) {
			return null ;
		}
		else {
			//print_r($row);
			$adulte = new Adulte($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom),utf8_encode($row->mdp),utf8_encode($row->email),utf8_encode($row->imgProfil),utf8_encode($row->estAdmin),utf8_encode($row->estConfirme));
			return $adulte ;
		}
	}
        
        // Trouver tous les adultes 
	public function findAllAdultes(/* myPDO */ $db) {
		$tabRes = array() ;
		$i = 0 ;
		$stmt = $db->prepare("SELECT * FROM Adulte ;");
		$res = $stmt->execute();
		while($row = $stmt->fetchObject()) {
			$adulte = new Adulte($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom),utf8_encode($row->mdp),utf8_encode($row->email),utf8_encode($row->imgProfil),utf8_encode($row->estAdmin),utf8_encode($row->estConfirme));
			$tabRes[$i] = $adulte ;
			$i++;
		}
		return $tabRes ;
	}
        
        // Trouver tous les adultes Non Confirmés
        public function findAllAdultesNC($db){
            $tabRes = array();
            $i = 0;
            $stmt = $db->prepare("SELECT * FROM Adulte where estConfirme = 0;");
            $res=$stmt->execute();
            while($row = $stmt->fetchObject()) {
                $adulte = new Adulte ($row->identifiant,utf8_encode($row->nom), utf8_encode($row->prenom),0,utf8_encode($row->email),utf8_encode($row->estConfirme));
                $tabRes[$i] =$adulte;
                $i++;
            }
            return $tabRes;
        }
        
                // Mise à jour d'un adulte lors de confirmation
           	public function updateAdulte(/* Adulte */ $adulte, /* myPDO */ $db) {
		try {
			$stmt = $db->prepare("UPDATE Adulte SET nom=:nom, prenom=:prenom, mdp=:mdp ,email=:email, imgProfil=:img ,estConfirme =:estConfirme WHERE identifiant=:id ;");
			$nom = utf8_decode($adulte->getNom());
			$prenom = utf8_decode($adulte->getPrenom());
                        $mdp = utf8_decode($adulte->getPassword());
                        $email = utf8_decode($adulte->getEmail());
                        $img = utf8_decode($adulte->getImage());
                        $estConfirme = utf8_decode($adulte->estConfirme());
			$id = $adulte->getIdentifiant();
			$stmt->bindParam(':nom', $nom);
			$stmt->bindParam(':prenom',$prenom);
                        $stmt->bindParam(':mdp',$mdp);
                        $stmt->bindParam(':email',$email);
                        $stmt->bindParam(':img',$img);
                        $stmt->bindParam(':id',$id);
                        $stmt->bindParam(':estConfirme',$estConfirme);
			$stmt->execute();
		} catch(Exception $e) {
			echo 'Exception re�ue : ',  $e->getMessage(), "\n";
		}
	}
        
        // mise à jour d'un adulte
        public function update(/* Adulte */ $adulte, /* myPDO */ $db) {
		try {
			$stmt = $db->prepare("UPDATE Adulte SET nom=:nom, prenom=:prenom,email=:email, imgProfil=:img WHERE identifiant=:id ;");
			$nom = utf8_decode($adulte->getNom());
			$prenom = utf8_decode($adulte->getPrenom());
                        $email = utf8_decode($adulte->getEmail());
                        $img = utf8_decode($adulte->getImage());
			$id = $adulte->getIdentifiant();
			$stmt->bindParam(':nom', $nom);
			$stmt->bindParam(':prenom',$prenom);
                        $stmt->bindParam(':email',$email);
                        $stmt->bindParam(':img',$img);
                        $stmt->bindParam(':id',$id);
			$stmt->execute();
		} catch(Exception $e) {
			echo 'Exception re�ue : ',  $e->getMessage(), "\n";
		}
	}
        
        // Mise à jour d'un adulte n'ayant pas une photo
        public function updateAdulteSansPhoto(/* Adulte */ $adulte, /* myPDO */ $db) {
		try {
			$stmt = $db->prepare("UPDATE Adulte SET nom=:nom, prenom=:prenom,email=:email WHERE identifiant=:id ;");
			$nom = utf8_decode($adulte->getNom());
			$prenom = utf8_decode($adulte->getPrenom());
                        $email = utf8_decode($adulte->getEmail());
			$id = $adulte->getIdentifiant();
			$stmt->bindParam(':nom', $nom);
			$stmt->bindParam(':prenom',$prenom);
                        $stmt->bindParam(':email',$email);
                        $stmt->bindParam(':id',$id);
			$stmt->execute();
		} catch(Exception $e) {
			echo 'Exception re�ue : ',  $e->getMessage(), "\n";
		}
	}
        
        // Trouver un adulte confirmé selon l'email et mot de passe 
        public function findPersonne(/* Personne */ $eleve, /* myPDO */ $db) {
		$stmt = $db->prepare("SELECT * FROM Adulte WHERE email = :email AND mdp = :mdp AND estConfirme = 1;");
		$email = utf8_decode($eleve->getEmail());
                $mdp = utf8_decode($eleve->getPassword());
                $stmt->bindParam(':email', $email);
                $stmt->bindParam(':mdp', $mdp);
		$res = $stmt->execute();
		$row = $stmt->fetchObject();
		if($row == null) {
			return null ;
		}
		else {
			//print_r($row);
			$pers = new Adulte($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom),utf8_encode($row->mdp),utf8_encode($row->email),utf8_encode($row->imgProfil),utf8_encode($row->estAdmin),utf8_encode($row->estConfirme));
			return $pers ;
		}
        }
        
        
        /* Afficher tous les adultes
	public function displayAllAdultes( $db) {
		$stmt = $db->prepare("SELECT * FROM Adulte ;");
		$res = $stmt->execute();
		echo "<table>";
		while($row = $stmt->fetchObject()) {
			echo "<tr>";
			echo "<td>".$row->identifiant."</td>";
			echo "<td>".$row->nom."</td>";
			echo "<td>".$row->prenom."</td>";
			echo "</tr>";
		}
		echo "</table>";
	}
         * 
         */
        
        // Supprimer un adulte
	public function deleteAdulte(/* Eleve */ $adulte, /* myPDO */ $db) {
		try {
			$stmt = $db->prepare("DELETE FROM Adulte WHERE identifiant = :id ;");
			$id = $adulte->getIdentifiant();
			$stmt->bindParam(':id', $id);
			$stmt->execute();
		} catch(Exception $e) {
			echo 'Exception reçue : ',  $e->getMessage(), "\n";
		}
	}
        
        
        // Supprimer les élèves appartenant à un adulte
        public function deleteAllEleve(/* Eleve */ $adulte, /* myPDO */ $db) {
		try {
			$stmt = $db->prepare("DELETE FROM etudiant_has_adulte WHERE Adulte_identifiant = :id ;");
			$id = $adulte->getIdentifiant();
			$stmt->bindParam(':id', $id);
			$stmt->execute();
		} catch(Exception $e) {
			echo 'Exception reçue : ',  $e->getMessage(), "\n";
		}
	}
        
      /*  
        public function addEleve ( $adulte,  $eleve, $db) {
		try {
			$stmt = $db->prepare("INSERT INTO etudiant_has_adulte (Etudiant_identifiant, Adulte_identifiant ) VALUES (:ide, :ida)");
	
                        $ida = utf8_decode($adulte->getIdentifiant());
                        $ide = utf8_decode($eleve->getIdentifiant());
			$stmt->bindParam(':ide', $ide);
			$stmt->bindParam(':ida',$ida);
			$stmt->execute();
		} catch(Exception $e) {
			echo 'Exception re�ue : ',  $e->getMessage(), "\n";
		}
	}
       
       */
        
       // Recherche d'un adulte  
        public function Rechercher($adulte, $db) {
        $tabRes = array();
        $i = 0;

        $nom = $adulte->getNom();
        $t = $nom."%";
        $stmt = $db->prepare("SELECT * FROM Adulte WHERE  estConfirme = 1 AND (nom LIKE :nom OR prenom LIKE :nom OR CONCAT (nom,' ',prenom) LIKE :nom OR CONCAT (prenom,' ',nom) LIKE :nom ) ;");
        $stmt->bindParam(':nom', $t);
        $res = $stmt->execute();
        while ($row = $stmt->fetchObject()) {
            $adulte = new Adulte($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom),utf8_encode($row->mdp),utf8_encode($row->email),utf8_encode($row->imgProfil),utf8_encode($row->estAdmin),utf8_encode($row->estConfirme));

            $tabRes[$i] = $adulte;
            $i++;
        }
        return $tabRes;
    }
        
        
}




?>